﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using Xceed.Silverlight.DragDrop;
using System.Windows.Media;

namespace Xceed.Silverlight.DataGrid
{
  #region Metadata

  [StyleTypedProperty( Property = "CellStyle", StyleTargetType = typeof( ColumnManagerCell ) )]

  #endregion //Metadata

  public class ColumnManagerRow : Row, IDropTarget
  {
    internal static readonly XceedRoutedEvent OnAllowColumnResizeChangedEvent = new XceedRoutedEvent();
    internal static readonly XceedRoutedEvent OnDefaultColumnTitleTemplateChangedEvent = new XceedRoutedEvent();

    public ColumnManagerRow()
      : base()
    {
      this.DefaultStyleKey = typeof( ColumnManagerRow );
    }

    #region AllowColumnSort Property

    public static readonly DependencyProperty AllowColumnSortProperty = DependencyProperty.Register(
      "AllowColumnSort",
      typeof( bool? ),
      typeof( ColumnManagerRow ),
      new PropertyMetadata( null, new PropertyChangedCallback( ColumnManagerRow.OnAllowColumnSortChanged ) ) );

    public bool? AllowColumnSort
    {
      get
      {
        return ( bool? )this.GetValue( ColumnManagerRow.AllowColumnSortProperty );
      }
      set
      {
        this.SetValue( ColumnManagerRow.AllowColumnSortProperty, value );
      }
    }

    private static void OnAllowColumnSortChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
    }

    #endregion

    #region AllowColumnResize Property

    public static readonly DependencyProperty AllowColumnResizeProperty = DependencyProperty.Register(
      "AllowColumnResize",
      typeof( bool? ),
      typeof( ColumnManagerRow ),
      new PropertyMetadata( null, new PropertyChangedCallback( ColumnManagerRow.OnAllowColumnResizeChanged ) ) );

    public bool? AllowColumnResize
    {
      get
      {
        return ( bool? )this.GetValue( ColumnManagerRow.AllowColumnResizeProperty );
      }
      set
      {
        this.SetValue( ColumnManagerRow.AllowColumnResizeProperty, value );
      }
    }

    private static void OnAllowColumnResizeChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
      var row = ( ColumnManagerRow )sender;
      row.EventManager.RaiseEvent( ColumnManagerRow.OnAllowColumnResizeChangedEvent, new XceedRoutedEventArgs( row ) );
    }

    #endregion

    #region AllowColumnReorder Property

    public static readonly DependencyProperty AllowColumnReorderProperty = DependencyProperty.Register(
      "AllowColumnReorder",
      typeof( bool? ),
      typeof( ColumnManagerRow ),
      new PropertyMetadata( null, new PropertyChangedCallback( ColumnManagerRow.OnAllowColumnReorderChanged ) ) );

    public bool? AllowColumnReorder
    {
      get
      {
        return ( bool? )this.GetValue( ColumnManagerRow.AllowColumnReorderProperty );
      }
      set
      {
        this.SetValue( ColumnManagerRow.AllowColumnReorderProperty, value );
      }
    }

    private static void OnAllowColumnReorderChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
    }

    #endregion // AllowColumnReorder Property

    #region DefaultColumnTitleTemplate Property

    public static readonly DependencyProperty DefaultColumnTitleTemplateProperty = DependencyProperty.Register(
      "DefaultColumnTitleTemplate",
      typeof( DataTemplate ),
      typeof( ColumnManagerRow ),
      new PropertyMetadata( null, new PropertyChangedCallback( ColumnManagerRow.OnDefaultColumnTitleTemplateChanged ) ) );

    public DataTemplate DefaultColumnTitleTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( ColumnManagerRow.DefaultColumnTitleTemplateProperty );
      }
      set
      {
        this.SetValue( ColumnManagerRow.DefaultColumnTitleTemplateProperty, value );
      }
    }

    private static void OnDefaultColumnTitleTemplateChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
      ColumnManagerRow row = ( ColumnManagerRow )sender;
      row.EventManager.RaiseEvent( ColumnManagerRow.OnDefaultColumnTitleTemplateChangedEvent, new XceedRoutedEventArgs( row ) );
    }

    #endregion //TitleTemplate Property

    internal override void Accept( IVisitor visitor )
    {
      visitor.Visit( this );
    }

    private DependencyObject GetParentDropTarget()
    {
      DependencyObject dropTargetComplement = this;
      while( ( dropTargetComplement != null ) && !( dropTargetComplement is IDropTargetComplement ) )
      {
        dropTargetComplement = VisualTreeHelper.GetParent( dropTargetComplement );
      }
      return dropTargetComplement;
    }

    #region IDropTarget Implementation

    bool IDropTarget.CanDropElement( DragDropContext dragContext )
    {
      DependencyObject dropTargetComplement = this.GetParentDropTarget();
      if( dropTargetComplement != null )
      {
        return ( ( IDropTargetComplement )dropTargetComplement ).CanDropElement( this, dragContext );
      }
      return false;
    }

    void IDropTarget.DragEnter( DragDropContext dragContext )
    {
    }

    void IDropTarget.DragOver( DragDropContext dragContext, Point mousePosition )
    {
      DependencyObject dropTargetComplement = this.GetParentDropTarget();
      if( dropTargetComplement != null )
      {
        ( ( IDropTargetComplement )dropTargetComplement ).DragOver( this, dragContext, mousePosition );
      }
    }

    void IDropTarget.DragLeave( DragDropContext dragContext )
    {
    }

    void IDropTarget.Drop( DragDropContext dragContext )
    {
      DependencyObject dropTargetComplement = this.GetParentDropTarget();
      if( dropTargetComplement != null )
      {
        ( ( IDropTargetComplement )dropTargetComplement ).Drop( this, dragContext );
      }
    }

    #endregion
  }
}
